/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

/**
 * A PowerUp or PowerDown
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class PowerUp extends GameObject {

    /**
     * In which second will the PowerUp be shown
     */
    private int showSecond;
    /**
     * In which second will the PowerUp be hidden
     */
    private int hideSecond;
    /**
     * The Position of the PowerUpd
     */
    private Vec2 position;
    /**
     * Is it currently shown
     */
    private boolean show;
    /**
     * A factor for the new mass of the player
     */
    private float newMass;
    /**
     * A factor for the new speed of the player
     */
    private float newSpeed;
    /**
     * The number of the PowerUp/Down
     */
    private int number;
    /**
     * The new control mode of the player
     */
    private GlobalSettings.ControlMode controlMode = GlobalSettings.ControlMode.Normal;

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param image
     * @param w
     */
    public PowerUp(int x, int y, Image image, World w) {
        super(x, y, image, w);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param image
     */
    public PowerUp(int x, int y, Image image) {
        super(x, y, image);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param w
     */
    public PowerUp(int x, int y, World w) {
        super(x, y, w);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     */
    public PowerUp(int x, int y) {
        super(x, y);
        init();
    }

    /**
     * Constructor
     *
     * @param image
     * @param w
     */
    public PowerUp(Image image, World w) {
        super(image, w);
        init();
    }

    /**
     * Constructor
     *
     * @param image
     */
    public PowerUp(Image image) {
        super(image);
        init();
    }

    /**
     * Constructor
     *
     * @param w
     */
    public PowerUp(World w) {
        super(w);
        init();
    }

    /**
     * Constructor
     */
    public PowerUp() {
        super();
        init();
    }

    /**
     * Init the Object
     */
    public final void init() {
        show = false;
    }

    /**
     * Set the Number
     * 
     * @param n 
     */
    public void setNumber(int n) {
        number = n;
    }
    
    /**
     * Return Number
     * @return number
     */
    public int getNumber() {
        return number;
    }
    /**
     * Set the physicle BodyPosition
     *
     * @param y
     * @param x
     */
    public void setBodyPosition(int x, int y) {
        bodyDef.position.set(x, y);
    }

    /**
     * *
     * Set the physicle body definition
     *
     * @param def
     */
    public void setBodyDef(BodyDef def) {
        bodyDef = def;
    }

    /**
     * *
     * get the physicle body definition
     *
     * @return physicle body definition
     */
    public BodyDef getBodyDef() {
        return bodyDef;
    }

    /**
     * Get the physicle fixture definition
     *
     * @return physicle fixture definition
     */
    public FixtureDef getFixtureDef() {
        return fd;
    }

    /**
     * *
     * Get the physicle body
     *
     * @return physicle body
     */
    public Body getBody() {
        return body;
    }

    /**
     * set the new force
     *
     * @param force
     * @param center
     */
    public void applyForce(Vec2 force, Vec2 center) {
        body.applyForce(force, center);
    }

    /**
     * Draw the PowerUp
     *
     * @param g
     */
    @Override
    public void draw(Graphics g) {
        g.drawImage(image, x - 10, y - 10);
    }

    /**
     * *
     * Setter
     *
     * @param time
     */
    public void setShowSecond(int time) {
        showSecond = time;
    }

    /**
     * *
     * Getter
     *
     * @return In which second will the PowerUp be shown
     */
    public int getShowSecond() {
        return showSecond;
    }

    /**
     * *
     * Setter
     *
     * @param time
     */
    public void setHideSecond(int time) {
        hideSecond = time;
    }

    /**
     * *
     * Getter
     *
     * @return In which second will the PowerUp be hidden
     */
    public int getHideSecond() {
        return hideSecond;
    }

    /**
     * *
     * Set the Position of PowerUpd
     *
     * @param pos
     */
    public void setPosition(Vec2 pos) {
        position = pos;
        x = (int) pos.x + 10;
        y = (int) pos.y + 10;
    }

    /**
     * *
     * Getter
     *
     * @return PowerUp position
     */
    public Vec2 getPosition() {
        return position;
    }

    /**
     * *
     * Setter
     *
     * @param s
     */
    public void setShow(boolean s) {
        if(s && GlobalSettings.currentGameMode == GlobalSettings.gameModes.Server) {
            StartShowPowerUp showSend = new StartShowPowerUp();
            showSend.x = position.x;
            showSend.y = position.y;
            showSend.number = number;
            showSend.start = showSecond;
            showSend.end = hideSecond;
            PlayerControl.server.sendToAllTCP(showSend);
            
        }else if(!s && GlobalSettings.currentGameMode == GlobalSettings.gameModes.Server) {
            EndShowPowerUp hideSend = new EndShowPowerUp();
            hideSend.number = number;
            PlayerControl.server.sendToAllTCP(hideSend);
        }
        show = s;
    }

    /**
     * *
     * Getter
     *
     * @return is the PowerUp shown
     */
    public boolean getShow() {
        return show;
    }

    /**
     * *
     * Set Parameter with positive effect
     */
    public void initPowerUp() {
        newMass = (float) Math.random() + 1;
        newSpeed = (float) Math.random() + 1;
        controlMode = GlobalSettings.ControlMode.Normal;
    }

    /**
     * *
     * Set Parameter with negative effect
     */
    public void initPowerDown() {
        newMass = (float) Math.random();
        newSpeed = (float) Math.random();
        if (Math.random() < 0.5) {
            controlMode = GlobalSettings.ControlMode.Dificult;
        } else {
            controlMode = GlobalSettings.ControlMode.Normal;
        }
    }

    /**
     * *
     * Get mass factor
     *
     * @return mass factor
     */
    public float getNewMass() {
        return newMass;
    }

    /**
     * *
     * Get speed factor
     *
     * @return speed factor
     */
    public float getNewSpeed() {
        return newSpeed;
    }

    /**
     * *
     * Get new controll mode
     *
     * @return new controll mode
     */
    public GlobalSettings.ControlMode getControlMode() {
        return controlMode;
    }
    
    public String toString() {
        return number + ";" +showSecond  + ";" + hideSecond + ";" + show + ";";
    }
}
